package com.sh.data.engine.domain.base.model.enums;

import com.sh.data.engine.common.enumDefinition.DSType;
import org.apache.commons.lang3.StringUtils;

/**
 * @author: mengzheng.mhc
 * @date: 2024/6/5 14:10
 */
public enum Database {

    ORACLE,
    MYSQL,
    POSTGRESQL,
    SQLSERVER,
    DB2,
    SAP,
    ESSQL,
    CLICKHOUSE,
    HIVE,
    DM,
    KINGBASE8,
    ANSI,
    TIDB,
    IoTDB,
    InfluxDB,
    AS400;

    public static Database from(String dbName) {
        for (Database db : values()) {
            if (StringUtils.equalsIgnoreCase(db.name(), dbName)) {
                return db;
            }
        }
        if (StringUtils.equalsIgnoreCase(DSType.MatrixDB.toString(), dbName)) {
            return Database.POSTGRESQL;
        }
        return null;
    }

    public boolean match(String dialectName) {
        return StringUtils.equalsIgnoreCase(dialectName, name());
    }

    public String getEscape() {
        return switch (this) {
            case MYSQL, TIDB, HIVE -> "`";
            case POSTGRESQL, SAP, ORACLE, KINGBASE8, DM -> "\"";
            default -> "";
        };
    }
}
